<?php

namespace api\controllers;

use api\components\BaseController;
use common\helpers\commonApi;
use common\helpers\PageApi;
use common\helpers\TransformApi;
use common\models\Activity;
use common\models\ActivityEnter;
use common\models\Carousel;
use common\models\Nav;
use common\models\System;
use Yii;

class HomeController extends BaseController
{
    /**
     * 轮播图
     * @return array
     */
    public function actionCarousel()
    {
        $position = (int)Yii::$app->request->get('position', 1);
        $data = Carousel::find()->where(['position' => $position, 'status' => 1])->select(['img', 'path'])->asArray()->all();
        foreach ($data as &$datum) {
            $datum['img'] = commonApi::getDomain($datum['img']);
            $datum['path'] = $datum['path']??'';
        }
        return commonApi::message('成功', true, $data);
    }

    /**
     * 系统配置
     * @return array
     */
    public function actionSystem()
    {
        $system = System::getModel();
        $data = [
            'phone' => $system->phone,
            'about_us' => $system->about_us,
            'name' => $system->apple_name,
            'vip_img' => commonApi::getDomain($system->vip_img),
        ];
        return commonApi::message('成功', true, $data);
    }

    /**
     * 导航
     * @return array
     */
    public function actionNav()
    {
        $data = Nav::find()
            ->select(['name','path','icon'])
            ->where(['status'=>1])
            ->orderBy(['sort'=>SORT_DESC,'create_time'=>SORT_DESC])
            ->asArray()
            ->all();
        foreach ($data as &$datum){
            $datum['icon'] = commonApi::getDomain($datum['icon']);
        }
        return commonApi::message('成功',true,$data);
    }

    /**
     * 近期活动
     * @return array
     */
    public function actionRecent()
    {
        $get = Yii::$app->request->get();
        $page = (int)$get['page']??1;
        $recent_day = System::getModel()->recent_day;
        $recent_day = $recent_day??0;
        $s = strtotime(date('Y-m-d'))+$recent_day*24*60*60;
        $s = date('Y-m-d H:i:s',$s);
        $query = Activity::find()
            ->select(['id as act_id','main_img','name','total','surplus','start_time','end_time','price'])
            ->where(['status'=>1,'delete'=>0])
            ->andWhere(['>','end_time',date('Y-m-d H:i:s')])
            ->andWhere(['<=','start_time',$s]);
        $data = PageApi::apiPage($query,$page);
        $act_ids = array_column($data['data'],'act_id');
        $enter = ActivityEnter::find()->where(['act_id'=>$act_ids,'status'=>1])->select(['act_id','sum(num) as num'])->groupBy(['act_id'])->asArray()->all();
        $enter = array_column($enter,'num','act_id');
        $activity_discount = System::getModel()->activity_discount??0;
        foreach ($data['data'] as &$datum){
            $y = $enter[$datum['act_id']]??0;
            $datum['surplus'] = $datum['total']-$y;
            $datum['main_img'] = commonApi::getDomain($datum['main_img']);
            $datum['start_time'] = date('Y-m-d',strtotime($datum['start_time']));
            $datum['end_time'] = date('Y-m-d',strtotime($datum['end_time']));
            $datum['vip_price'] = $datum['price'];
            if ($activity_discount>0){
                $datum['vip_price'] = $datum['price']*($activity_discount/10);
                $datum['vip_price'] = TransformApi::decimalPoint($datum['vip_price']);
            }
            $datum['registered'] = $datum['total']-$datum['surplus'];
        }
        return commonApi::message('成功',true,$data);
    }

    /**
     * 推荐位
     * @return array
     */
    public function actionRecommend()
    {
        $data = Activity::find()
            ->select(['recommend_img','id as act_id'])
            ->where(['status'=>1,'delete'=>0,'is_recommend'=>1])
            ->limit(2)
            ->asArray()->all();
        foreach ($data as &$datum){
            $datum['recommend_img'] = commonApi::getDomain($datum['recommend_img']);
        }
        return commonApi::message('成功',true,$data);
    }
}